<?php
##############################################
#
#   地区管理
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\LocalityModel;
use Admin\Model\AdminBaseModel;

class LocalityController extends AdminBaseController {
	
	protected $_localityObject;
	
	public function _initialize(){
		parent::_initialize();
		$this -> _localityObject  = new LocalityModel();
	}
	
	/**
	 * 地区列表
	 */
	public function localityList(){
		if(!IS_GET) return;
			$locality = $this -> _localityObject -> getLocality();
			$data = $this -> _localityObject -> disData($locality);
			$this -> assign('locality', $data);
			$this -> display('locality_list');
	}
	
	/**
	 * 添加地区
	 */
	public function addLocality(){
		if(!IS_POST) return;
		$data = I('post.');
		$data['pid'] = intval($data['pid']);
		$data['sort'] = intval($data['sort']);
		$data['is_display'] = intval($data['is_display']);
		$admin_base_model = new AdminBaseModel('locality');
		$result = $admin_base_model -> addData($data);
		if($result)
			$this -> success('添加成功', U('localityList'), 1);
		else
			$this -> error('操作失败', '', 1);
	}
	
	/**
	 * 删除地区
	 */
	public function deleteLocality(){
		if(!IS_AJAX) return;
		$result = $this -> _localityObject -> deleteLocality(intval($_GET['lid']));
		if($result == 1)
			$this -> ajaxReturn(array('status' => 1,'message' => '该地区有子地区，不可删除'));
		elseif($result == 2)
			$this -> ajaxReturn(array('status' => 2,'message' => '删除成功'));
		elseif($result == 3)
			$this -> ajaxReturn(array('status' => 3,'message' => '操作失败'));
	}
	
}